﻿#pragma once


#include <QMainWindow>
#include <QTimer>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QDir>
#include <QMessageBox>
#include <QWidget>
#include <QApplication>
#include <QHeaderView>
#include <QMenuBar>
#include <QPushButton>
#include <QTableWidget>
#include <QTextEdit>
#include <QStringList>
#include <QFileDialog>
#include <QSettings>
#include "QDesktopServices"
#include "QUrl"
#include "QListView"
#include "QStringListModel"
#include "QDesktopServices"
#include "QStringListModel"
#include <QProcess>
#include <QDockWidget>
#include <QDateTime>
#include <QTcpSocket>

using namespace std;
#include <iostream>
#include <string.h>

#include "modbus.h"

#include "spdlog/spdlog.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/fmt/ostr.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
//extern std::shared_ptr<spdlog::logger> logger ;

#include "camera_qdialog.h"
#include "qt_hread_process.h"
#include "login_dialog.h"
#include "show_qlabel.h"
#include "my_table_widget.h"
#include "commonfunction.h"
#include "devicesizetable.h"

//extern QImage matToQImage(const Mat mat_in);

#include "dl_3.h";


#include "com.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    //COM
    QString m_com_port;



    //存图线程
    int m_save_ratio = 1000;
    SaveImageThread* m_save_image_thread;
    QString m_save_image_dir;

    QString m_io_ip="";
    mutex mutex_io;

    int m_camera_number=0;///<成员变量m_camera_number,该视觉系统中，相机的总数量

     vector<bool> m_vector_show_image;
    //主界面 图像显示
    QWidget* m_image_widget;
    vector< ShowQLabel*> m_vector_show_qlabel;
//    vector< QPictureBox*> m_vector_show_qlabel;
    //检测方法设置界面
    QDialog* m_qdialog_detection = nullptr;
    QTabWidget* m_tabwidget_detection = nullptr;
    vector<QtThreadProcess*>  m_vector_detection_thread;

    //相机设置界面
    QDialog* m_qdialog_camera = nullptr;
    QTabWidget* m_tabwidget_camera = nullptr;
    vector<CameraBaseQDialog*> m_vector_camera;

    int m_online=0;///在线连接相机检测时，该值为1，离线检测时，该值为0

    QSettings* m_settings_para = nullptr;
    QString m_para_dir;
    QString m_exe_directory;
    QString m_type_dir = "type";
    QString m_type_name= "";
    QString m_save_data_dir="";

    // dockwidget pannel
    QDockWidget* m_qdockwidget_pannel= nullptr;
    QPushButton *m_qpushbutton_start_detection  = nullptr;
    QPushButton *m_qpushbutton_stop_detection  = nullptr;
    QPushButton *m_qpushbutton_para  = nullptr;
    QPushButton *m_qpushbutton_camera  = nullptr;
    QPushButton *m_qpushbutton_open_image_dir  = nullptr;
    QPushButton *m_qpushbutton_open_data_dir  = nullptr;
    QPushButton *m_qpushbutton_open_model_dir  = nullptr;
    QPushButton *m_qpushbutton_open_help  = nullptr;
    QPushButton *m_qpushbutton_exit  = nullptr;

    // dockwidget m_qdockwidget_devicesizetable
    QDockWidget* m_qdockwidget_devicesizetable= nullptr;

    //modibus
    int m_unuse_use_modbus=0;
    modbus_t *m_modbus_tcp;
    QString  m_modbus_ip;
    int      m_modbus_port;
    QTimer*  m_timer_plc  = nullptr;
    QStringList m_save_data_time;
    vector<int> m_plc_trigger;
    vector<int> m_plc_message;
    vector<int> m_plc_result;

    //QTcpSocket
    int m_qtcpsocket_unused_used =0;
    QTcpSocket* m_qtcpsocket     = nullptr;
    QString  m_qtcpsocket_ip     = "127.0.0.1";
    int     m_qtcpsocket_port    = 502;

    //登录对话框
    QString m_user_passward;
    QString m_admin_passeard;
    QString m_senior_passeard;
    int m_login_privilege = 0;
    Login_Dialog* m_Login_Dialog;



public :
    void login();

    void startDetection();

    void stopDetection();

    void clickPara();

    void clickCamera();

    void closeSoftware();

    void returnMainFrame();

    virtual void closeEvent(QCloseEvent* event) override;

    void saveImage(ShowResult result);
    void showResult(ShowResult result);

    void timerLoadImage();

    void timerPLC();

    bool loadPara();

    void initdockDevicesizetable();

    void initdockPannel();

    void initMainWindow();

    void initImageControl();

    void initQSQLITE(QString path)
    {
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

        db.setDatabaseName(path);
		if (!db.open()) {
			QMessageBox::critical(nullptr, path, "SQLite open error");
		}
    }
};

#pragma execution_character_set("utf-8")
